﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Database;
using QLK.Web.App_Code;
using QLK.Database.BLLs;

namespace QLK.Web
{
    public partial class Customers : System.Web.UI.Page
    {
        private string _CustID;
        private CustomerBLL CustBLL = new CustomerBLL();
        private Customer Cust;

        protected void Page_Load(object sender, EventArgs e)
        {
            _CustID = Request.QueryString[Columns.CustomerID];
            if (!IsPostBack)
            {
                Load_Data();
            }
        }

        private void Load_Data()
        {
            if (string.IsNullOrEmpty(_CustID))
            {
                btnGoTo.Enabled = true;
                txtCusName.Text = null;
                txtAddress.Text = null;
                txtPhonenumber.Text = null;

                txtCusName.Focus();
            }
            else
            {
                btnGoTo.Enabled = false;
                txtCusName.Text = Request.QueryString[Columns.CustomerName];
                txtAddress.Text = Request.QueryString[Columns.Address];
                txtPhonenumber.Text = Request.QueryString[Columns.PhoneNumber];
            }
        }

        protected void btnViewCustomer_Click(object sender, EventArgs e)
        {
            Response.Redirect(PageUrls.CustomerDetails);
        }

        protected void btnGoTo_Click(object sender, EventArgs e)
        {
            int ID = CustBLL.CustomerGetIDByName(txtCusName.Text.ToString());
            if (ID == 0)
                lblWarning.Text = "CustomerID not found";
            else
            {
                Response.Redirect(string.Format("{0}?{1}={2}", PageUrls.ExportInvoices, Columns.CustomerID, ID));
            }            
        }

        protected void btnSaveCus_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_CustID))
            {
                Cust = new Customer();
                Cust.CustomerName = txtCusName.Text.ToString();
                Cust.Address = txtAddress.Text.ToString();
                Cust.PhoneNumber = txtPhonenumber.Text.ToString();

                if (CustBLL.CustomerInsert(Cust))
                {
                    lblWarning.Text = "Insert successfully!";
                }
                else
                {
                    lblWarning.Text = "Insert failed!";
                }
            }
            else
            {
                Cust = new Customer();
                Cust.CustomerID = int.Parse(_CustID);
                Cust.CustomerName = txtCusName.Text.ToString();
                Cust.Address = txtAddress.Text.ToString();
                Cust.PhoneNumber = txtPhonenumber.Text.ToString();

                if (CustBLL.CustomerUpdate(Cust))
                {
                    lblWarning.Text = "Update successfully!";
                }
                else
                {
                    lblWarning.Text = "Update failed!";
                }
            }
        }
    }
}
